package yqt.com.cnki.controller;


import haha.life.common.CutPageBean;
import jzqb.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yqt.com.cnki.bo.DataBaseBo;
import yqt.com.cnki.po.DataBasePo;
import yqt.com.cnki.po.SubmitFieldPo;
import yqt.com.cnki.service.DIYTagService;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/DIYTag")
public class DIYTagController {
    @Autowired
    DIYTagService dIYTagService;

    @GetMapping("/getAllDBbase")
    public Result getAllDBbase() {
        return Result.succ(dIYTagService.getAllDBbase());
    }

    @GetMapping("/getNotSysFieldByDBcode")
    public Result getNotSysFieldByDBcode(@RequestParam(value = "dbCode") String  dbCode) {
        System.out.println("--------"+dbCode);
        return Result.succ(dbCode);
    }

    @GetMapping("/list")
    public Result layoutname(
            @PathParam("DATABASENAME") String DATABASENAME,
            @PathParam("orderType") String orderType,
            @PathParam("orderValue") String orderValue,
            @PathParam("page") int page,
            @PathParam("pageSize") int pageSize
    ) {
        DataBaseBo dataBaseBo = new DataBaseBo();
        dataBaseBo.setDATABASENAME(DATABASENAME);
        dataBaseBo.setOrderType(orderType);
        dataBaseBo.setOrderValue(orderValue);
        dataBaseBo.setPage(page);
        dataBaseBo.setPageSize(pageSize);
        dataBaseBo.setStartPage((page-1)*pageSize);

        CutPageBean<DataBaseBo> cutPageBean = dIYTagService.cutPage(dataBaseBo);
        return Result.succ(cutPageBean);
    }

    @GetMapping("/getNotSysFieldsState")
    public Result getNotSysFieldsState(
            @PathParam("DBcode") String DBcode
    ){
        List<SubmitFieldPo> res = dIYTagService.getNotSysFieldsState(DBcode);
        return Result.succ(res);
    }

    @PostMapping("/showToCustomerChange")
    public Result showToCustomerChange(
            @RequestBody Map<String,String> map
    ) {
        String dbInfo = map.get("dbInfo");
        String dbCode = dbInfo.split(",")[0];
        String dbName = dbInfo.split(",")[1];
        String newState = map.get("newState");

        DataBasePo dataBasePo = new DataBasePo();
        dataBasePo.setDATABASECODE(dbCode);
        dataBasePo.setDATABASENAME(dbName);
        if (newState.equals("true")){
            dataBasePo.setShowToCustomer(true);
        }else{
            dataBasePo.setShowToCustomer(false);
        }

        Boolean res = dIYTagService.showToCustomerChange(dataBasePo);
        return Result.succ("cutPageBean");
    }

    @PostMapping("/save")
    public Result save(
            @RequestBody HashMap<String,Object> map
    ) {
        String dbCode = (String) map.get("dbCode");
        String dbName = (String) map.get("dbName");
//        Map<String, ArrayList<Map<String,String>>> fields = (Map<String, ArrayList<Map<String,String>>>)map.get("fields");
        ArrayList<Map<String ,String>> fields = (ArrayList<Map<String ,String>>)map.get("fields");
        ArrayList<SubmitFieldPo> params = new ArrayList<>();


        for (int i = 0; i < fields.size(); i++) {
            Map<String, String> o = fields.get(i);
            SubmitFieldPo submitFieldPo = new SubmitFieldPo();
            submitFieldPo.setDbCode(dbCode);
            submitFieldPo.setDbName(dbName);
            submitFieldPo.setFieldName(o.get("field"));
            submitFieldPo.setFieldDisplaydName(o.get("fieldDisplaydName"));
            submitFieldPo.setFieldType(String.valueOf((o.get("fieldType"))));
            submitFieldPo.setDefaultValue(o.get("defaultValue"));
            submitFieldPo.setIsAllowNull(String.valueOf(o.get("isAllowNull")));
            submitFieldPo.setIsOnlyOne(String.valueOf(o.get("isOnlyOne")));
            submitFieldPo.setDescript(o.get("descript"));
            submitFieldPo.setSortNum(String.valueOf(i+1));
            params.add(submitFieldPo);
        }

        Boolean res = dIYTagService.save(dbCode,params);
        return Result.succ(res);
    }
}
